package com.jdjw.modules.thirdpost.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jdjw.common.ResponseResult;
import com.jdjw.modules.thirdpost.entity.ThirdPostExceptionLog;
import com.jdjw.modules.thirdpost.param.PageList;
import com.jdjw.modules.thirdpost.param.ThirdExceptionParam;
import com.jdjw.modules.thirdpost.service.ThirdPostExceptionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
public class ThirdExceptionLogController {

    @Autowired
    ThirdPostExceptionService thirdPostExceptionService;

    @ResponseBody
    @RequestMapping(value = "list", method = RequestMethod.POST)
    @ApiOperation("异常日志列表")
    public Map list(@RequestBody PageList queryEntity) {
        Map<String, Object> map = new HashMap<>();
        try {
            IPage<ThirdPostExceptionLog> page = thirdPostExceptionService.list(queryEntity);
            map.put("data", page.getRecords());
            map.put("count", page.getTotal());
            map.put("code", 0);
            map.put("msg", "请求成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 500);
            map.put("msg", "请求失败");
            map.put("data", "");
            map.put("count", 0);
        }
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "retry", method = RequestMethod.POST)
    @ApiOperation("异常日志重试")
    public ResponseResult retry(@RequestBody ThirdExceptionParam thirdExceptionParam) {
        ResponseResult result = new ResponseResult();
        try {
            String msg = thirdPostExceptionService.retry(thirdExceptionParam);
            result.setCode(200);
            result.setMsg("SUCCESS");
            result.setData(msg);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
            result.setMsg("ERROR");
            result.setData("请求失败");
        }
        return result;
    }
}
